import { Chat } from "@/main";
import TencentCloudChat from "@tencentcloud/chat";
import TUIChatEngine from "@tencentcloud/chat-uikit-engine";
import { defineStore } from "pinia";
import { ref } from "vue";

export const useChatDataStore = defineStore("ChatData", () => {
  const groupNum = ref(0);
  const unReadNum = ref(0);
  const setGroupNum = (number: number) => {
    groupNum.value = number;
  };

  const initChat = () => {
    if (TUIChatEngine.isReady()) {
      unReadNum.value = Chat.getTotalUnreadMessageCount();
      const onTotalUnreadMessageCountUpdated = (event: any) => {
        unReadNum.value = event.data;
      };
      Chat.on(
        TencentCloudChat.EVENT.TOTAL_UNREAD_MESSAGE_COUNT_UPDATED,
        onTotalUnreadMessageCountUpdated
      );
    }
  };

  return {
    groupNum,
    setGroupNum,
    unReadNum,
    initChat,
  };
});
